{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "1.1、python的数据类型：数字、字符串、列表、字典、元组、集合"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "1.2、pip install matplotlib（可以指定安装源）"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "1.3、python中序列是指一块可存放多个值的存储空间，序列中的值按照一定的顺序排列，可以按照值的编号（索引）\n",
    "来访问他们\n",
    "列表、元组、字典、集合、字符串"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "1.4、 空集合：a = set（）\n",
    "    空字典：a = {}"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "1.5、集合是一个无序、不重复且不支持索引的序列\n",
    "    集合支持集合对象支持消除重复元素，还支持union (联合)、intersection(交), difference (差)和sysmmetric difference (对称差集)等数学运算\n",
    "    https://blog.csdn.net/qq_46089299/article/details/105643650"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "1.6 import ...as  ...\n",
    "    from ... import ...(as)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法1中：153是一个水仙花数\n",
      "方法1中：370是一个水仙花数\n",
      "方法1中：371是一个水仙花数\n",
      "方法1中：407是一个水仙花数\n",
      "******************************\n",
      "方法2中：153是一个水仙花数\n",
      "方法2中：370是一个水仙花数\n",
      "方法2中：371是一个水仙花数\n",
      "方法2中：407是一个水仙花数\n"
     ]
    }
   ],
   "source": [
    "#1.7 题目要求验证这个数是不是水仙花数，现对题目加深，要求输出所有的三位数水仙花数\n",
    "\n",
    "# a = input(\"请输入一个整数：\")\n",
    "# a = int(a)\n",
    "# #需要取出这个数的百、十、个位\n",
    "# i = int(a/100)\n",
    "# j = int(a/10%10)\n",
    "# k = a%10\n",
    "# print(i,j,k)\n",
    "# all = i**3 + j**3 + k**3\n",
    "# print(all)\n",
    "# if all == a:\n",
    "#     print(\"{}是一个水仙花数\".format(a))\n",
    "# else:\n",
    "#     print('{}不是一个水仙花数'.format(a))\n",
    "import math\n",
    "\n",
    "for i in range(100,1000):\n",
    "    a = i\n",
    "    #需要a这个数的百、十、个位\n",
    "    i = int(a/100) #取百位\n",
    "    j = int(a/10%10)#取十位\n",
    "    k = a%10#取各位\n",
    "    all = i**3 + j**3 + k**3\n",
    "    if all == a:\n",
    "        print(\"方法1中：{}是一个水仙花数\".format(a))\n",
    "    else:\n",
    "        continue\n",
    "print('*'*30)\n",
    "for i in range(100,1000):\n",
    "    sum = pow(int(str(i)[0]),3) + pow(int(str(i)[1]),3) + pow(int(str(i)[2]),3)\n",
    "    if sum == i:\n",
    "        print(\"方法2中：{}是一个水仙花数\".format(i))\n",
    "    else:\n",
    "        continue\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "858\n",
      "原数字是858，新的数字是808\n"
     ]
    }
   ],
   "source": [
    "#1.8 随机产生一个三位整数，将十位化为0\n",
    "#也可以使用1.7方法2的方式，依旧繁琐，只是将数值型数据转化为字符型进行索引\n",
    "\n",
    "import random\n",
    "num = random.randint(100,1000)\n",
    "print(num)\n",
    "#按照1.7的方法对1.8进行探究\n",
    "#那我们就取出百位和个位，组成新的数字\n",
    "i = int(num/100)\n",
    "k = num%10\n",
    "new_num = i*100 + 0 *10 + k\n",
    "print(\"原数字是{}，新的数字是{}\".format(num,new_num))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "#1.9\n",
    "\n",
    "# 大于1000，对1000进行整除取出千位及以上的数字，否则输出和\n",
    "x,y,z = input('请输入三个数字：').split() #从键盘输入的数字为字符型，所以下面需要转换为整型\n",
    "sum = int(x)**2 + int(y)**2 + int(z)**2\n",
    "if sum >  1000:\n",
    "    new_num = int(sum/1000)\n",
    "    print(new_num)\n",
    "else:\n",
    "    print(sum)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "此时的运费为1820.0\n"
     ]
    }
   ],
   "source": [
    "#1.10\n",
    "\n",
    "# 总运费的计算公式f=pws（1-d）\n",
    "#以距离s和折扣d来决定最后的f，意味着p和w都是常量\n",
    "s = int(input('请输入距离：'))\n",
    "p = 1\n",
    "w = 1\n",
    "d = 0\n",
    "if s < 250:\n",
    "    d= 0\n",
    "    f = p*w*s*(1-d)\n",
    "    print('此时的运费为{}'.format(f))\n",
    "else:\n",
    "    if  250 <=s< 500:\n",
    "        d= 0.025\n",
    "        f = p*w*s*(1-d)\n",
    "        print('此时的运费为{}'.format(f))\n",
    "    else:\n",
    "        if  500<=s< 1000:\n",
    "            d = 0.045\n",
    "            f = p*w*s*(1-d)\n",
    "            print('此时的运费为{}'.format(f))\n",
    "        else:\n",
    "            if  1000 <=s < 2000:\n",
    "                d = 0.075\n",
    "                f = p*w*s*(1-d)\n",
    "                print('此时的运费为{}'.format(f))\n",
    "            else:\n",
    "                if  2000 <=s < 2500:\n",
    "                    d = 0.090\n",
    "                    f = p*w*s*(1-d)\n",
    "                    print('此时的运费为{}'.format(f))\n",
    "                else:\n",
    "                    if  2500 <=s < 3000:\n",
    "                        d = 0.12\n",
    "                        f = p*w*s*(1-d)\n",
    "                        print('此时的运费为{}'.format(f))\n",
    "                    else:\n",
    "                        d = 0.15\n",
    "                        f = p*w*s*(1-d)\n",
    "                        print('此时的运费为{}'.format(f))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您输入的是：2019年3月4日\n",
      "今天是周1\n"
     ]
    }
   ],
   "source": [
    "#1.11\n",
    "\n",
    "#通过用户输入年、月、日三个变量，确定输入日期是周几\n",
    "#注意公式是对7取余，所以得分类\n",
    "y = int(input('请输入年份：'))\n",
    "m = int(input('请输入月份：'))\n",
    "d = int(input('请输入日期：'))\n",
    "print('您输入的是：{}年{}月{}日'.format(y,m,d))\n",
    "y0 = y - (14-m)//12\n",
    "x = y0 + y0//4 - y0//100 + y0//400\n",
    "m0 = m + 12*((14-m)//12)-2\n",
    "d0 = (d + x + (31 + m0)//12)%7\n",
    "if d0  == 0:\n",
    "    print('今天是周日')\n",
    "else:\n",
    "    print(\"今天是周{}\".format(d0))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每个月应还款额为1036.3840875701705元\n"
     ]
    }
   ],
   "source": [
    "#1.12\n",
    "\n",
    "#输入：年限N，年利率r，贷款金额P；输出：每月还款金额\n",
    "N = int(input('请输入贷款年限：'))\n",
    "r = float(input('请输入贷款年利率：'))\n",
    "P = int(input('请输入贷款金额：'))\n",
    "N_ = 12*N\n",
    "r_ = r/12\n",
    "huankuan = (P*r_*((1+r_)**N_)) / (((1+r_)**N_)-1)\n",
    "print(\"每个月应还款额为{}元\".format(huankuan))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2419.0212272045333\n"
     ]
    }
   ],
   "source": [
    "#1.13\n",
    "\n",
    "#经纬度的问题，注意弧度值与角度值的转换\n",
    "import math\n",
    "R = 6370\n",
    "x1 = math.radians(int(input('请输入A点的经度：')))#radians函数求弧长\n",
    "y1 = math.radians(int(input('请输入A点的纬度：')))\n",
    "x2 = math.radians(int(input('请输入B点的经度：')))\n",
    "y2 = math.radians(int(input('请输入B点的纬度：')))\n",
    "d = R * math.acos(math.cos(x1-x2)*math.cos(y1)*math.cos(y2)+math.sin(y1)*math.sin(y2))\n",
    "print(d)\n",
    "#https://www.cnblogs.com/xinmomoyan/p/10864521.html"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6是一个完数\n",
      "28是一个完数\n",
      "496是一个完数\n"
     ]
    }
   ],
   "source": [
    "#1.14\n",
    "\n",
    "#先确定一个数是否是完数，再求1000以内的所有完数\n",
    "#我尝试用列表来解决\n",
    "# num = int(input('请输入一个数'))\n",
    "# list1= []\n",
    "# sum = 0\n",
    "# for i in range(1,num):\n",
    "#     if num % i ==0:\n",
    "#         list1.append(i)\n",
    "#     else:\n",
    "#         continue\n",
    "# for i in list1:\n",
    "#     sum +=i\n",
    "# if sum == num:\n",
    "#     print('{}是一个完数'.format(num))\n",
    "for i in range(1,1000):\n",
    "    list1= []\n",
    "    sum = 0\n",
    "    for j in range(1,i):\n",
    "        if i % j ==0:\n",
    "            list1.append(j)\n",
    "        else:\n",
    "            continue\n",
    "    for k in list1:\n",
    "        sum += k\n",
    "    if sum == i:\n",
    "        print('{}是一个完数'.format(i))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<zip object at 0x000001A8328E3880>\n",
      "(1, 2, 3) (-1, -2, -3)\n"
     ]
    }
   ],
   "source": [
    "#1.15\n",
    "\n",
    "x = [1,2,3]\n",
    "y = [-1,-2,-3]\n",
    "print(zip(*zip(x,y)))\n",
    "a,b = zip(*zip(x,y))\n",
    "print(a,b)\n",
    "#直接输出表达式是地址，可以使用list(zip(...))将Zip对象转换为列表\n",
    "#zip(*)相当于给zip函数解压，即将元组分散为列表\n",
    "#https://www.runoob.com/python/python-func-zip.html"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大写的个数为1\n",
      "小写的个数为1\n",
      "数字的个数为4\n",
      "其它字符的个数为4\n",
      "('G',)\n",
      "('h',)\n",
      "('1', '2', '3', '4')\n",
      "('*', '*', '*', '*')\n"
     ]
    }
   ],
   "source": [
    "#1.16\n",
    "\n",
    "#四个变量：大写字母、小写字母、数字、其他字符，最后需将分类结果以元组形式返回（元组一定程度上可当做只读列表）\n",
    "#思来想去没弄明白，结果发现python已经封装了判断类型的函数isdigit，isupper，islower\n",
    "big = []\n",
    "small = []\n",
    "dig = []\n",
    "others = []\n",
    "str1 = input('请输入一串字符串')\n",
    "for i in str1:\n",
    "    if i.isdigit():\n",
    "        dig.append(i)\n",
    "    else:\n",
    "        if i.isupper():\n",
    "            big.append(i)\n",
    "        else:\n",
    "            if i.islower():\n",
    "                small.append(i)\n",
    "            else:\n",
    "                others.append(i)\n",
    "print('大写的个数为{}'.format(len(big)))\n",
    "print('小写的个数为{}'.format(len(small)))\n",
    "print('数字的个数为{}'.format(len(dig)))\n",
    "print('其它字符的个数为{}'.format(len(others)))\n",
    "print(tuple(big))\n",
    "print(tuple(small))\n",
    "print(tuple(dig))\n",
    "print(tuple(others))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 => zero\n",
      "5 => five\n",
      "19 => nineteen\n",
      "10 => ten\n",
      "20 => twenty\n",
      "23 => twenty three\n",
      "30 => thirty\n",
      "100 => one hundred\n",
      "700 => seven hundred\n",
      "711 => seven hundred and eleven\n",
      "729 => seven hundred and twenty nine\n"
     ]
    }
   ],
   "source": [
    "#1.17\n",
    "\n",
    "#将数字转换为英文表示形式，分析发现英文1-20是专用单词，20之后才是组合单词。\n",
    "#使用从大到小依次剥离的方式，即从千位、百位、十位、个位依次剥离，输出相应的表达形式\n",
    "#另外还可以使用CSDN上的方法：https://blog.csdn.net/Changxing_J/article/details/107998766\n",
    "#csdn上的按照英文数字书写的方式，三位一组，从而依次剥离\n",
    "def unit_to_word(u):\n",
    "    convert_table = {\n",
    "        0: \"zero\",\n",
    "        1: \"one\",\n",
    "        2: \"two\",\n",
    "        3: \"three\",\n",
    "        4: \"four\",\n",
    "        5: \"five\",\n",
    "        6: \"six\",\n",
    "        7: \"seven\",\n",
    "        8: \"eight\",\n",
    "        9: \"nine\",\n",
    "    }\n",
    "    return convert_table[u]\n",
    "def tens_to_word(t):\n",
    "    convert_table = {\n",
    "        0: \"\",\n",
    "        10: \"ten\",\n",
    "        11: \"eleven\",\n",
    "        12: \"twelve\",\n",
    "        13: \"thirteen\",\n",
    "        14: \"fourteen\",\n",
    "        15: \"fifteen\",\n",
    "        16: \"sixteen\",\n",
    "        17: \"seventeen\",\n",
    "        18: \"eighteen\",\n",
    "        19: \"nineteen\",\n",
    "        2: \"twenty\",\n",
    "        3: \"thirty\",\n",
    "        4: \"forty\",\n",
    "        5: \"fifty\",\n",
    "        6: \"sixty\",\n",
    "        7: \"seventy\",\n",
    "        8: \"eighty\",\n",
    "        9: \"ninety\",\n",
    "    }\n",
    "    if 9 < t < 20:\n",
    "        return convert_table[t]\n",
    "    else:\n",
    "        if t <= 9:\n",
    "            return unit_to_word(t)\n",
    "        else:\n",
    "            if t%10 ==0:\n",
    "                return convert_table[int(t/10)]\n",
    "            else:\n",
    "                tens = unit_to_word(t%10)#计算余数\n",
    "            return convert_table[int(t/10)]+\" \"+tens\n",
    "def hundreds_to_word(h):\n",
    "    if h > 99:\n",
    "        word = unit_to_word(int(h/100)) + \" hundred\"\n",
    "        tens = h % 100\n",
    "        if tens == 0:\n",
    "            return word\n",
    "        else:\n",
    "            return word + \" and \" + tens_to_word(tens)\n",
    "    else:\n",
    "        return tens_to_word(h)\n",
    "for test in [0, 5, 19,10, 20,23,30, 100, 700, 711, 729]:\n",
    "    print(test, \"=>\", hundreds_to_word(test))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}